package kr.saeildamil.androidforstudy2.ch31;

import java.util.ArrayList;

import kr.saeildamil.androidforstudy2.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.GestureDetector.OnDoubleTapListener;
import android.widget.TextView;

public class GestureDump extends Activity {
	ArrayList<String> arGesture;
	TextView mResult;
	GestureDetector mDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arGesture = new ArrayList<String>();
		setContentView(R.layout.ch31_gesturedump);
		
		mResult = (TextView) findViewById(R.id.ch31_gesturedump_result02);
		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setOnDoubleTapListener(mDoubleTapListener);
	
	}
	
	OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener() {
		
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			appendText("onSingleTapConfirmed");
			return false;
		}
		
		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			appendText("onDoubleTapEvent");
			return false;
		}
		
		@Override
		public boolean onDoubleTap(MotionEvent e) {
			appendText("onDoubleTap");
			return false;
		}
	};
	
	OnGestureListener mGestureListener = new OnGestureListener() {
		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			appendText("onSingleTapUp");
			return false;
		}
		
		@Override
		public void onShowPress(MotionEvent e) {
			appendText("onShowPress");
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			String text = String.format("Scroll : (%d, %d) to (%d, %d) distance : (%d, %d)", (int)e1.getX(), (int)e1.getY(), (int)e2.getX(), (int)e2.getY(), (int)distanceX, (int)distanceY);
			appendText(text);
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			appendText("onLongPress");
			
		}
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			String text = String.format("Fling : (%d,  %d) Velocity : (%d, %d)", (int)e1.getX(), (int)e1.getY(), (int)e2.getX(), (int)e2.getY(), (int)velocityX, (int)velocityY);
			appendText(text);
			return false;
		}
		
		@Override
		public boolean onDown(MotionEvent e) {
			String text = String.format("onDown : %d,  %d",(int)e.getX(), (int)e.getY());
			appendText(text);
			return false;
		}
	};
	
	private void appendText(String text){
		if(arGesture.size() > 15){
			arGesture.remove(0);
		}
		arGesture.add(text);
		StringBuilder sb = new StringBuilder();
		for(String str : arGesture){
			sb.append(str + "\n");
		}
		mResult.setText(sb.toString());
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	}
}
